﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Remoting.Messaging;

namespace Rogan.DBUtility
{
    public sealed class DbHelper
    {
        
        private DbHelper()
        {
        }

        /// <summary>
        /// 获取当前线程中的DbHelperSQL
        /// </summary>
        /// <returns></returns>
        private static DbHelperSQL GetCurrentDbHelperSql()
        {
            DbHelperSQL dbHelperSql = CallContext.GetData("DbHelperSQL") as DbHelperSQL;
            if (dbHelperSql == null)
            {
                dbHelperSql = new DbHelperSQL();
                CallContext.SetData("DbHelperSQL", dbHelperSql);
            }
            return dbHelperSql;
        }

        #region 连接方法
        /// <summary>
        /// 用应用默认的连接建立一个连接，并将连接放到线程数据管道中
        /// Open总是创建一个新的连接
        /// </summary>
        /// <returns></returns>
        //public static void Open()
        //{
        //    DbHelperSQL deHelperSql = GetCurrentDbHelperSql();
        //    if (deHelperSql != null)
        //    {
        //        deHelperSql.Open();
        //        return;
        //    }
        //}


        /// <summary>
        /// 关闭连接，并清除数据管道中的连接对象
        /// </summary>
        /// <returns></returns>
        //public static void Close()
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql(); // GetExternalNGDbTrans();
        //    if (dbHelperSql != null)
        //    {
        //        dbHelperSql.Close();
        //        return;
        //    }
        //}
        #endregion

        #region 事务处理
        /// <summary>
        /// 开始事务
        /// </summary>
        public static bool BeginTran()
        {
            return BeginTran(IsolationLevel.ReadCommitted);
        }

        /// <summary>
        /// 开始事务
        /// </summary>
        /// <param name="isolevel"></param>
        private static bool BeginTran(IsolationLevel isolevel)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();

            return dbHelperSql.BeginTran(isolevel);
        }

        /// <summary>
        /// 提交事务
        /// </summary>
        public static bool CommitTran()
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();

            return dbHelperSql.CommitTran();
        }

        /// <summary>
        /// 回滚事务
        /// </summary>
        public static bool RollbackTran()
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();

            return dbHelperSql.RollBackTran();
        }
        #endregion

        #region 公用方法
        ///// <summary>
        ///// 判断是否存在某表的某个字段
        ///// </summary>
        ///// <param name="tableName">表名称</param>
        ///// <param name="columnName">列名称</param>
        ///// <returns>是否存在</returns>
        //public static bool ColumnExists(string tableName, string columnName)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    return dbHelperSql.ColumnExists(tableName, columnName);
        //}
        public static int GetMaxID(string FieldName, string TableName)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.GetMaxID(FieldName, TableName);
        }
        public static bool Exists(string strSql)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.Exists(strSql);
        }
        ///// <summary>
        ///// 表是否存在
        ///// </summary>
        ///// <param name="TableName"></param>
        ///// <returns></returns>
        //public static bool TabExists(string TableName)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    return dbHelperSql.TabExists(TableName);
        //}
        public static bool Exists(string strSql, params SqlParameter[] cmdParms)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.Exists(strSql, cmdParms);
        }
        #endregion

        #region  执行简单SQL语句

        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.ExecuteSql(SQLString);
        }

        //public static int ExecuteSqlByTime(string SQLString, int Times)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    return dbHelperSql.ExecuteSqlByTime(SQLString, Times);
        //}

        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>		
        public static int ExecuteSqlTran(List<String> SQLStringList)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.ExecuteSqlTran(SQLStringList);
        }
        /// <summary>
        /// 执行带一个存储过程参数的的SQL语句。
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <param name="content">参数内容,比如一个字段是格式复杂的文章，有特殊符号，可以通过这个方式添加</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, string content)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.ExecuteSql(SQLString, content);
        }
        ///// <summary>
        ///// 执行带一个存储过程参数的的SQL语句。
        ///// </summary>
        ///// <param name="SQLString">SQL语句</param>
        ///// <param name="content">参数内容,比如一个字段是格式复杂的文章，有特殊符号，可以通过这个方式添加</param>
        ///// <returns>影响的记录数</returns>
        //public static object ExecuteSqlGet(string SQLString, string content)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    return dbHelperSql.ExecuteSqlGet(SQLString, content);
        //}
        /// <summary>
        /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
        /// </summary>
        /// <param name="strSQL">SQL语句</param>
        /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.ExecuteSqlInsertImg(strSQL, fs);
        }

        /// <summary>
        /// 执行一条计算查询结果语句，返回查询结果（object）。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果（object）</returns>
        public static  object GetSingle(string SQLString)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.GetSingle(SQLString);
        }
        //public static object GetSingle(string SQLString, int Times)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    return dbHelperSql.GetSingle(SQLString, Times);
        //}
        /// <summary>
        /// 执行查询语句，返回SqlDataReader ( 注意：调用该方法后，一定要对SqlDataReader进行Close )
        /// </summary>
        /// <param name="strSQL">查询语句</param>
        /// <returns>SqlDataReader</returns>
        public static SqlDataReader ExecuteReader(string strSQL)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.ExecuteReader(strSQL);
        }
        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.Query(SQLString);
        }
        //public static DataSet Query(string SQLString, int Times)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    return dbHelperSql.Query(SQLString, Times);
        //}
        #endregion

        #region 执行带参数的SQL语句

        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.ExecuteSql(SQLString, cmdParms);
        }

        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">SQL语句的哈希表（key为sql语句，value是该语句的SqlParameter[]）</param>
        public static void ExecuteSqlTran(Hashtable SQLStringList)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            dbHelperSql.ExecuteSqlTran(SQLStringList);
        }
        ///// <summary>
        ///// 执行多条SQL语句，实现数据库事务。
        ///// </summary>
        ///// <param name="SQLStringList">SQL语句的哈希表（key为sql语句，value是该语句的SqlParameter[]）</param>
        //public static int ExecuteSqlTran(List<CommandInfo> cmdList)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    return dbHelperSql.ExecuteSqlTran(cmdList);
        //}

        ///// <summary>
        ///// 执行多条SQL语句，实现数据库事务。
        ///// </summary>
        ///// <param name="SQLStringList">SQL语句的哈希表（key为sql语句，value是该语句的SqlParameter[]）</param>
        //public static void ExecuteSqlTranWithIndentity(System.Collections.Generic.List<CommandInfo> SQLStringList)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    dbHelperSql.ExecuteSqlTranWithIndentity(SQLStringList);
        //}
        ///// <summary>
        ///// 执行多条SQL语句，实现数据库事务。
        ///// </summary>
        ///// <param name="SQLStringList">SQL语句的哈希表（key为sql语句，value是该语句的SqlParameter[]）</param>
        //public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    dbHelperSql.ExecuteSqlTranWithIndentity(SQLStringList);
        //}
        /// <summary>
        /// 执行一条计算查询结果语句，返回查询结果（object）。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果（object）</returns>
        public static object GetSingle(string SQLString, params SqlParameter[] cmdParms)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.GetSingle(SQLString, cmdParms);
        }

        /// <summary>
        /// 执行查询语句，返回SqlDataReader ( 注意：调用该方法后，一定要对SqlDataReader进行Close )
        /// </summary>
        /// <param name="strSQL">查询语句</param>
        /// <returns>SqlDataReader</returns>
        public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.ExecuteReader(SQLString, cmdParms);
        }

        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString, params SqlParameter[] cmdParms)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.Query(SQLString, cmdParms);
        }

        #endregion

        #region 存储过程操作

        /// <summary>
        /// 执行存储过程，返回SqlDataReader ( 注意：调用该方法后，一定要对SqlDataReader进行Close )
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>SqlDataReader</returns>
        public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.RunProcedure(storedProcName, parameters);
        }


        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="tableName">DataSet结果中的表名</param>
        /// <returns>DataSet</returns>
        public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
        {
            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.RunProcedure(storedProcName, parameters, tableName);
        }
        //public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
        //{
        //    DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
        //    return dbHelperSql.RunProcedure(storedProcName, parameters, tableName, Times);
        //}


        /// <summary>
        /// 执行存储过程，返回影响的行数		
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="rowsAffected">影响的行数</param>
        /// <returns></returns>
        public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
        {

            DbHelperSQL dbHelperSql = GetCurrentDbHelperSql();
            return dbHelperSql.RunProcedure(storedProcName, parameters, out rowsAffected);
        }
        #endregion
    }
}
